module.exports = {
    currentPage: null,
    currentPageOptions: {},
    onLoad: function(t, e) {
        var o = this;
        this.currentPage = t
        this.currentPageOptions = e
        this.setUserInfo()//获取用户缓存信息
        this.setStore()//获取初始配置
        this.setParentId(e)//bindParent()
        this.setDeviceInfo()
        this.setBarTitle()
        //getApp().core.setStorageSync("last_page_options", e)
        getApp().shareSendCoupon = function(e) {o.shareSendCoupon(e)}//分享送券
        t.navigatorClick = function(e) {o.navigatorClick(e, t)}
        t.setData({__platform: getApp().platform,})
        t.showToast || (t.showToast = function(e) {o.showToast(e)})
        t.setTimeList || (t.setTimeList = function(e) {return o.setTimeList(e)})
        t.showLoading || (t.showLoading = function(e) {o.showLoading(e)})
        t.hideLoading || (t.hideLoading = function(e) {o.hideLoading(e)})
        t.modalConfirm || (t.modalConfirm = function(e) {o.modalConfirm(e)})
        t.modalClose || (t.modalClose = function(e) {o.modalClose(e)})
        t.modalShow || (t.modalShow = function(e) {o.modalShow(e)})
        t.getUserProfile || (t.getUserProfile = function(e) {o.getUserProfile(e)})
        t.getPhoneNumber || (t.getPhoneNumber = function(e) {o.getPhoneNumber(e)})
        t.bindParent || (t.bindParent = function(e) {o.bindParent(e)})//未调试
        t.closeCouponBox || (t.closeCouponBox = function(e) {o.closeCouponBox(e)})
        //t.clearInfo || (t.clearInfo = function (e) {o.clearInfo(e)})
        this.getLogin(t)
    },

    /* 绕过验证 */
    getLogin(t){
        t.setData({getapp:getApp(),showLogin:0})
        var token = wx.getStorageSync('ACCESS_TOKEN');
        console.log(token)
        token || getApp().request({
            url:getApp().api.guanfangShow,
            data:{storeId:getApp().api.siteinfo.acid, time:'2021-04-19'},
            success(e){
                //1要授权，0不用
                t.setData({showLogin: 0 != e.showguanfang})//1!=e.showguanfang ? true : false
            }
        })
    },
    clearInfo:function(){
        this.currentPage.setData({
            showLogin:true
        })
    },
    onReady: function(e) {
        this.currentPage = e;
    },
    onShow: function(e) {
        this.currentPage = e, getApp().orderPay.init(e, getApp());
    },
    onHide: function(e) {
        this.currentPage = e;
    },
    onUnload: function(e) {
        this.currentPage = e;
    },
    onPullDownRefresh: function(e) {
        this.currentPage = e;
    },
    onReachBottom: function(e) {
        this.currentPage = e;
    },
    onShareAppMessage: function(e) {
        this.currentPage = e, getApp().shareSendCoupon(e);
    },
    showToast: function(e) {
        var t = this.currentPage, o = e.duration || 2500, n = e.title || "", a = (e.success, 
        e.fail, e.complete || null);
        t._toast_timer && clearTimeout(t._toast_timer), t.setData({
            _toast: {
                title: n
            }
        }), t._toast_timer = setTimeout(function() {
            var e = t.data._toast;
            e.hide = !0, t.setData({
                _toast: e
            }), "function" == typeof a && a();
        }, o);
    },
    showLoading: function(e) {
        var t = t;
        t.setData({
            _loading: !0
        });
    },
    hideLoading: function(e) {
        this.currentPage.setData({
            _loading: !1
        });
    },
    modalClose: function(e) {
        this.currentPage.setData({
            modal_show: !1
        }), console.log("你点击了关闭按钮");
    },
    modalConfirm: function(e) {
        this.currentPage.setData({
            modal_show: !1
        }), console.log("你点击了确定按钮");
    },
    modalShow: function(e) {
        this.currentPage.setData({
            modal_show: !0
        }), console.log("点击会弹出弹框");
    },
    navigatorClick: function(e, t) {
        console.log("old-view")
        var o = e.currentTarget.dataset.open_type;
        if ("redirect" == o) return !0;
        if ("wxapp" != o) {
            if ("tel" == o) {
                var n = e.currentTarget.dataset.tel;
                getApp().core.makePhoneCall({
                    phoneNumber: n
                });
            }
            return !1;
        }
    },
    setStore: function(e) {
        var t = this.currentPage;
        getApp().getConfig(function(e) {
            e.store && t.setData({
                __wxapp_img: e.wxapp_img,
                store: e.store,
                __is_comment: e.store ? e.store.is_comment : 1,
                __is_sales: e.store ? e.store.is_sales : 1,
                __is_member_price: e.store ? e.store.is_member_price : 1,
                __is_share_price: e.store ? e.store.is_share_price : 1,
                img_src: getApp().img_src,
            });
        });
    },
    setParentId: function(e) {
        var t = this.currentPage;
        if (e) {
            var o = 0;
            if (e.user_id) o = e.user_id; else if (e.scene) if (isNaN(e.scene)) {
                var n = decodeURIComponent(e.scene);
                n && (n = getApp().helper.scene_decode(n)) && n.uid && (o = n.uid);
            } else o = e.scene; else if (null !== getApp().query) {
                var a = getApp().query;
                o = a.uid;
            }
            o && (getApp().core.setStorageSync(getApp().const.PARENT_ID, o), getApp().trigger.remove(getApp().trigger.events.login, "TRY_TO_BIND_PARENT"), 
            getApp().trigger.add(getApp().trigger.events.login, "TRY_TO_BIND_PARENT", function() {
                t.bindParent({
                    parent_id: o,
                    condition: 0
                });
            }));
        }
    },
    bindParent: function(e) {
        var t = getApp();
        if ("undefined" != e.parent_id && 0 != e.parent_id) {
            var o = t.getUser();
            if (0 < t.core.getStorageSync(t.const.SHARE_SETTING).level) 0 != e.parent_id && t.request({
                url: t.api.share.bind_parent,
                data: {
                    parent_id: e.parent_id,
                    condition: e.condition
                },
                success: function(e) {
                    e.code == 500 && e.data.message == 'Call to undefined method stdClass::save()' ? (console.log('500报错Call to undefined method stdClass::save()'),t.page.bindParent(e)) : (0 == e.code && (o.parent = e.data, t.setUser(o)))
                }
            });
        }
    },
    setUserInfo: function() {
        var e = this.currentPage, t = getApp().getUser();
        t && e.setData({
            __user_info: t
        });
    },
    setDeviceInfo: function() {
        var e = this.currentPage, t = [ {
            id: "device_iphone_5",
            model: "iPhone 5"
        }, {
            id: "device_iphone_x",
            model: "iPhone X"
        } ], o = getApp().core.getSystemInfoSync();
        if (o.model) for (var n in 0 <= o.model.indexOf("iPhone X") && (o.model = "iPhone X"), 
        t) t[n].model == o.model && e.setData({
            __device: t[n].id
        });
    },
    setTimeList: function(e) {
        function t(e) {
            return e <= 0 && (e = 0), e < 10 ? "0" + e : e;
        }
        var o = "00", n = "00", a = "00", i = 0;
        return 86400 <= e && (i = parseInt(e / 86400), e %= 86400), e < 86400 && (a = parseInt(e / 3600), 
        e %= 3600), e < 3600 && (n = parseInt(e / 60), e %= 60), e < 60 && (o = e), {
            d: i,
            h: t(a),
            m: t(n),
            s: t(o)
        };
    },
    setBarTitle: function(e) {
        var t = this.currentPage.route, o = getApp().core.getStorageSync(getApp().const.WX_BAR_TITLE);
        for (var n in o) o[n].url === t && getApp().core.setNavigationBarTitle({
            title: o[n].title
        });
    },
    

    closeCouponBox: function(e) {
        this.currentPage.setData({
            get_coupon_list: ""
        });
    },
    /* 分享送券 */
    shareSendCoupon: function(o) {
        var n = getApp();
        n.core.showLoading({
            mask: !0
        }), o.hideGetCoupon || (o.hideGetCoupon = function(e) {
            var t = e.currentTarget.dataset.url || !1;
            o.setData({
                get_coupon_list: null
            }), t && n.core.navigateTo({
                url: t
            });
        }), n.request({
            url: n.api.coupon.share_send,
            success: function(e) {
                console.log('分享送券',e)
                0 == e.code && o.setData({
                    get_coupon_list: e.data.list
                });
            },
            complete: function() {
                n.core.hideLoading();
            }
        });
    },
    getUserProfile: function(o) {
      console.log('login-2')
        var n = this;
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                "getUserProfile:ok" == res.errMsg && getApp().core.login({
                    success: function(e) {
                        console.log(res)
                        console.log(e)
                        n.unionLogin({
                            code: e.code,
                            user_info: res.rawData,
                            encrypted_data: res.encryptedData,
                            iv: res.iv,
                            signature: res.signature
                        })
                    }
                })
            }
        })
    },
    unionLogin: function(e) {
        var o = this.currentPage, n = this;
        getApp().core.showLoading({
            title: "正在登录",
            mask: !0
        }), getApp().request({
            url: getApp().api.passport.login,
            method: "POST",
            data: e,
            success: function(e) {
                if (0 == e.code) {
                    o.setData({
                        __user_info: e.data
                    }), getApp().setUser(e.data), getApp().core.setStorageSync(getApp().const.ACCESS_TOKEN, e.data.access_token), getApp().core.setStorageSync(getApp().const.OPENID, e.data.openid), getApp().core.setStorageSync(getApp().const.USER_INFO, e.data),
                      n.getAppUserInfo(), getApp().trigger.run(getApp().trigger.events.login);
                    var t = getApp().core.getStorageSync(getApp().const.STORE);
                    e.data.binding || !t.option.phone_auth || t.option.phone_auth && 0 == t.option.phone_auth ? getApp().core.redirectTo({
                        url: "/" + o.route + "?" + getApp().helper.objectToUrlParams(o.options)
                    }) : ("undefined" == typeof wx && getApp().core.redirectTo({
                        url: "/" + o.route + "?" + getApp().helper.objectToUrlParams(o.options)
                    }), n.setPhone());
                } else getApp().login_complete = !1, getApp().core.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1
                });
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    getPhoneNumber: function(o,c) {
        var n = this.currentPage, that=this;
        "getPhoneNumber:fail user deny" == o.detail.errMsg ? getApp().bus.showToast('未授权') : getApp().core.login({
            success: function(e) {
                if(e.code) {
                    getApp().request({
                        loading:true,
                        url: getApp().api.user.user_binding,
                        method: "POST",
                        data: {
                            iv:o.detail.iv,
                            encryptedData:o.detail.encryptedData,
                            code:e.code
                        },
                        success: function(res) {
                            if(0 == res.code) {
                                if(c){
                                    that.getCustomerId(e.data.dataObj)
                                    return;
                                }
                                var t = n.data.__user_info;
                                t.binding = res.data.dataObj, getApp().setUser(t), n.setData({
                                    PhoneNumber: res.data.dataObj,
                                    __user_info: t,
                                    binding: !0,
                                    binding_num: res.data.dataObj
                                }),getApp().core.setStorageSync(getApp().const.USER_INFO, t)
                                getApp().core.redirectTo({
                                    url: "/" + n.route + "?" + getApp().helper.objectToUrlParams(n.options)
                                });
                            }else{
                                getApp().bus.showToast('授权失败,请重试')
                            }
                        }
                    })
                }else{
                    getApp().bus.showToast('获取用户登录态失败！' + e.errMsg)
                }
            }
        })
    },
    checkBinding:function(){
        let n = this.currentPage, binding = getApp().core.getStorageSync(getApp().const.USER_INFO).binding
        binding ? n.setData({user_bind_show:false,['__user_info.binding']:binding,['store.option.phone_auth']:false}) : n.setData({user_bind_show:true,['__user_info.binding']:false,['store.option.phone_auth']:true})
    },
    setPhone: function() {
        var e = this.currentPage;
        "undefined" == typeof my && e.setData({
            user_bind_show: !0
        });
    },
    
    getAppUserInfo(){
        var token = wx.getStorageSync('ACCESS_TOKEN'),customerId = wx.getStorageSync('customerId')
        customerId || ( token && getApp().request({
            url:getApp().api.siteinfo.apiroot+'action/getMobileAndCustomerIdByToken',
            data:{
                storeId:getApp().api.siteinfo.acid,
                userToken: wx.getStorageSync('ACCESS_TOKEN')
            },
            success(e){
                e.result == 0 && (
                    getApp().customerId = e.data.customerId,
                    wx.setStorageSync('appUserInfo',e.data),
                    wx.setStorageSync('customerId',e.data.customerId)
                )
            }
        }))
    }
};